package MovingWindowSeries;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FindAnagramsSolution_hot100_438 {
    public List<Integer> findAnagrams(String s, String p) {
        int lenS = s.length();
        int lenP = p.length(); // 窗口长度
        if (lenS < lenP) return new ArrayList<>();
        List<Integer> result = new ArrayList<>();
        int[] countS = new int[26]; // 字符串存放对应字母的个数
        int[] countP = new int[26];

        for (int i = 0; i < lenP; i++) {
            countS[s.charAt(i) - 'a']++;
            countP[p.charAt(i) - 'a']++;
        }

        if (Arrays.equals(countS, countP)) {
            result.add(0);
        }

        for (int i = 0; i < lenS - lenP; i++) {
            countS[s.charAt(i + lenP) - 'a']++;
            countS[s.charAt(i) - 'a']--;
            if (Arrays.equals(countS, countP)) {
                result.add(i + 1);
            }
        }
        return result;
    }
}
